//
//  DHCommunityView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/5/6.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHCommunityView.h"
#import "DHCommunity_NoImageCell.h"
#import "DHCommunityDetailController.h"

#pragma mark - CommunityTabView
@interface CommunityTabView : UIView
@property (strong, nonatomic) UIButton *hotButton; //热门
@property (strong, nonatomic) UIButton *latestButton; //最新
@property (strong, nonatomic) UIButton *followButton; //关注
@property (strong, nonatomic) UIView *lineView; //线条
@end

@implementation CommunityTabView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.hotButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, frame.size.height)];
        [self.hotButton setTitle:@"热门" forState:UIControlStateNormal];
        self.hotButton.titleLabel.font = [UIFont systemFontOfSize:15];
        self.hotButton.tag = 0;
        [self.hotButton addTarget:self action:@selector(tabChangeAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.hotButton setTitleColor:[UIColor colorWithHexString:@"#4E4E4E"] forState:UIControlStateNormal];
        [self addSubview:self.hotButton];
        
        self.latestButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width / 3, 0, frame.size.width / 3, frame.size.height)];
        [self.latestButton setTitle:@"最新" forState:UIControlStateNormal];
        self.latestButton.titleLabel.font = [UIFont systemFontOfSize:15];
        self.latestButton.tag = 1;
        [self.latestButton addTarget:self action:@selector(tabChangeAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.latestButton setTitleColor:[UIColor colorWithHexString:@"#4E4E4E"] forState:UIControlStateNormal];
        [self addSubview:self.latestButton];
        
        self.followButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width / 3 * 2, 0, frame.size.width / 3, frame.size.height)];
        [self.followButton setTitle:@"关注" forState:UIControlStateNormal];
        self.followButton.titleLabel.font = [UIFont systemFontOfSize:15];
        self.followButton.tag = 2;
        [self.followButton addTarget:self action:@selector(tabChangeAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.followButton setTitleColor:[UIColor colorWithHexString:@"#4E4E4E"] forState:UIControlStateNormal];
        [self addSubview:self.followButton];
        
        
        self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height - 2, self.hotButton.width / 3, 2)];
        self.lineView.backgroundColor = [UIColor colorWithHexString:@"#414141"];
        self.lineView.centerX = self.latestButton.centerX;
        [self addSubview:self.lineView];
    }
    return self;
}

- (void)tabChangeAction:(UIButton *)sender{
    switch (sender.tag) {
        case 0:{
            self.lineView.centerX = self.hotButton.centerX;
            [self routerEventWithName:@"hot" userInfo:nil];
            break;
        }
        case 1:{
            self.lineView.centerX = self.latestButton.centerX;
            [self routerEventWithName:@"latest" userInfo:nil];
            break;
        }
        case 2:{
            self.lineView.centerX = self.followButton.centerX;
            [self routerEventWithName:@"follow" userInfo:nil];
            break;
        }
            
        default:
            break;
    }
}

@end

#pragma mark - DHCommunityView
@interface DHCommunityView()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) CommunityTabView *tabView;
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *itemArr;
@property (assign, nonatomic) NSInteger indexPage;
@property (strong, nonatomic) NSString *type; //类型

@end

@implementation DHCommunityView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tabView = [[CommunityTabView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 40)];
        [self addSubview:self.tabView];
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, frame.size.width, frame.size.height - 40)];
        [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        [self.tableView setShowsVerticalScrollIndicator:NO];
        [self.tableView registerNib:[UINib nibWithNibName:@"DHCommunity_NoImageCell" bundle:nil] forCellReuseIdentifier:kDHCommunity_NoImageCell];
        [self addSubview:self.tableView];
        self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadCommunityData)];
        self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreCommunityData)];
        
        self.type = @"最新";
    }
    return self;
}

- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    if ([eventName isEqualToString:@"hot"]) {
        self.type = @"热门";
    }else if ([eventName isEqualToString:@"latest"]){
        self.type = @"最新";
    }else if ([eventName isEqualToString:@"follow"]){
        self.type = @"关注";
    }else if ([eventName isEqualToString:@"nice_changed"] || [eventName isEqualToString:@"follow_changed"]) { // 点赞、关注状态改变
        NSInteger row = [[userInfo objectForKey:@"row"] integerValue];
        [self.itemArr replaceObjectAtIndex:row withObject:[userInfo objectForKey:@"model"]];
        return;
    }
    [self.tableView.mj_header beginRefreshing];
}

- (void)loadLatestData{
    [self loadCommunityData];
}

- (void)loadCommunityData{
    self.indexPage = 0;
    [self.itemArr removeAllObjects];
    [self.tableView.mj_footer resetNoMoreData];
    NSDictionary *params;
    params = @{@"type":self.type,@"pageIndex":@(self.indexPage),@"pageSize":@(20)};
    [[DHNetAPIClient shareDHNetAPIClient] request_social_posts_squaresWithParams:params andBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        if (data) {
            [self.itemArr addObjectsFromArray:[data objectForKey:@"posts"]];
            if (self.itemArr.count < 20) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableView reloadData];
        }
    }];
}

- (void)loadMoreCommunityData{
    
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    NSArray *images = [model objectForKey:@"images"];
    
    //计算文字高度
    CGFloat contentHeight = [[model objectForKey:@"content"] heightForFont:[UIFont systemFontOfSize:14] width:kScreenWidth - 20];
    //如果内容中有 \n 等转义字符
    NSInteger specialChar = 0;//出现次数
    specialChar = [[model objectForKey:@"content"] countOccurencesOfString:@"\n"];
    CGFloat textHeight = contentHeight + specialChar * 14;
    
    //图片CollectionView高度
    CGFloat imgsHeight = 0;
    if (images.count == 0) {
        imgsHeight = 0;
    }else if (images.count > 0 && images.count <= 3){
        imgsHeight = 128;
    }else if (images.count > 3 && images.count <= 6){
        imgsHeight = 128 * 2 + 5;
    }else if (images.count > 6 && images.count <= 9){
        imgsHeight = 128 * 3 + 10;
    }
    
    return imgsHeight + textHeight + 133;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHCommunity_NoImageCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHCommunity_NoImageCell forIndexPath:indexPath];
    [cell updateCell:[self.itemArr objectAtIndex:indexPath.row]];
    cell.row = indexPath.row;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    DHCommunityDetailController *vc = [[DHCommunityDetailController alloc] initWithCommunityDic:model];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

- (NSMutableArray *)itemArr{
    if (!_itemArr) {
        _itemArr = [NSMutableArray array];
    }
    return _itemArr;
}

@end
